package com.able.wanandroid.module.home;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.able.wanandroid.R;
import com.able.wanandroid.app.Constants;
import com.able.wanandroid.base.BaseFragment;
import com.able.wanandroid.bean.ArticleBean;
import com.able.wanandroid.bean.BannerBean;
import com.able.wanandroid.databinding.FragmentHomeBinding;
import com.able.wanandroid.module.webview.WebActivity;
import com.able.wanandroid.utils.GlideImageLoader;
import com.able.wanandroid.utils.LogUtils;
import com.able.wanandroid.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Able
 * @date 2018/3/20
 */
public class HomeFragment extends BaseFragment<FragmentHomeBinding> implements
        SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    private HomeViewModel mHomeViewModel;
    private ArrayList<ArticleBean.DataBean.DatasBean> mArticleList;
    private ArticleAdapter mArticleAdapter;
    private List<BannerBean.DataBean> mBannerList;
    private Banner mBanner;
    private ArrayList<String> mImagePaths;
    private ArrayList<String> mBannerTitles;
    private int mPageNo;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initView();
        onRefresh();
    }

    private void initView() {
        mBinding.swipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mBinding.swipeRefreshLayout.setOnRefreshListener(this);
        initAdapter();

        addHeaderView();

        mImagePaths = new ArrayList<>();
        mBannerTitles = new ArrayList<>();
    }

    private void initAdapter() {
        mArticleList = new ArrayList<>();
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mArticleAdapter = new ArticleAdapter(R.layout.item_home_list, mArticleList);
        mArticleAdapter.setOnLoadMoreListener(this, mBinding.recyclerView);
        mArticleAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mBinding.recyclerView.setAdapter(mArticleAdapter);

        mArticleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ArticleBean.DataBean.DatasBean article = mArticleList.get(position);
                goWebView(article.getTitle(), article.getLink());
            }
        });

        mArticleAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ToastUtils.showError(getContext(), position + "");

                mArticleAdapter.notifyItemChanged(position + 1);
            }
        });
    }

    private void goWebView(String title, String link) {
        Intent intent = new Intent(getContext(), WebActivity.class);
        intent.putExtra(Constants.EXTRA_WEB_TITLE, title);
        intent.putExtra(Constants.EXTRA_WEB_URL, link);
        startActivity(intent);
    }

    private void addHeaderView() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.item_banner, (ViewGroup) mBinding.recyclerView.getParent(), false);
        mBanner = view.findViewById(R.id.banner);
        mArticleAdapter.addHeaderView(view);
    }

    @Override
    public void onRefresh() {
        mPageNo = 0;
        getArticleData();
        getBannerData();
    }

    @Override
    public void onLoadMoreRequested() {
        mPageNo++;
        loadMoreArticle();
    }

    private void loadMoreArticle() {
        mHomeViewModel
                .loadMoreArticle(mPageNo)
                .observe(HomeFragment.this, new Observer<ArticleBean>() {
                    @Override
                    public void onChanged(@Nullable ArticleBean articleBean) {
                        List<ArticleBean.DataBean.DatasBean> datas = articleBean.getData().getDatas();
                        mArticleList.addAll(datas);
                        mArticleAdapter.loadMoreComplete();

                        String title = datas.get(0).getTitle();
                        LogUtils.i("loadMoreArticle", "第 " + mPageNo + " 页 -> " + title);
                    }
                });
    }

    private void getArticleData() {
        mHomeViewModel = ViewModelProviders.of(HomeFragment.this).get(HomeViewModel.class);
        mHomeViewModel
                .queryArticle()
                .observe(HomeFragment.this, new Observer<ArticleBean>() {
                    @Override
                    public void onChanged(@Nullable ArticleBean articleBean) {
                        List<ArticleBean.DataBean.DatasBean> datas = articleBean.getData().getDatas();
                        mArticleList.clear();
                        mArticleList.addAll(datas);
                        mArticleAdapter.notifyDataSetChanged();
                        mBinding.swipeRefreshLayout.setRefreshing(false);
                        LogUtils.i("getArticleData", "条数 " + datas.size());

                    }
                });
    }

    private void getBannerData() {
        mHomeViewModel
                .getBanner()
                .observe(HomeFragment.this, new Observer<BannerBean>() {
                    @Override
                    public void onChanged(@Nullable BannerBean bannerBean) {
                        mBannerList = bannerBean.getData();
                        mImagePaths.clear();
                        mBannerTitles.clear();
                        for (int i = 1; i < bannerBean.getData().size(); i++) {
                            mImagePaths.add(mBannerList.get(i).getImagePath());
                            mBannerTitles.add(mBannerList.get(i).getTitle());
                        }
                        LogUtils.i("getBannerData", "地址 " + mImagePaths.get(0));
                        setBannerAdapter();
                    }
                });
    }

    private void setBannerAdapter() {
        mBanner
                .setImages(mImagePaths)
                .setImageLoader(new GlideImageLoader())
                .setBannerTitles(mBannerTitles)
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        ToastUtils.showInfo(getActivity(), mBannerTitles.get(position));
                    }
                })
                .start();
    }


}
